/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Views;

import Controllers.Main;
import Models.FileRead;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JSpinner;
import javax.swing.SpinnerDateModel;
import javax.swing.text.DateFormatter;

/**
 *
 * @author Nicolas_2
 */
public class SimulationParameters extends javax.swing.JFrame {

    private final FileRead fileReader;
    private final String folderEnding = "_EU_pitch";
    private final String fileExtension = ".txt";
    private String selectedDate = null;
    private String selectedValue = null;
    private final String ROOT_FOLDER_NAME = "Generated Data";
    private static final SimpleDateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");
    private String selectedTime;

    /**
     * Creates new form FileSelection
     */
    public SimulationParameters() {
        initComponents();
        this.setResizable(false);
        this.setTitle("File Selection");
        fileReader = new FileRead();
        initialiseDateComboBox(fileReader);

        JSpinner.DateEditor timeEditor = new JSpinner.DateEditor(SpinnerTime, "HH:mm:ss");
        DateFormatter formatter = (DateFormatter) timeEditor.getTextField().getFormatter();
        formatter.setAllowsInvalid(false); // Make sure no invalid input is allowed
        SpinnerTime.setEditor(timeEditor);
        String StartTime = "08:30:00";
        Date time;
        try {
            time = new SimpleDateFormat("HH:mm:ss").parse(StartTime);
            SpinnerTime.setValue(time); // will only show the current time
        } catch (ParseException ex) {
            Logger.getLogger(SimulationParameters.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        LabelDate = new javax.swing.JLabel();
        LabelValue = new javax.swing.JLabel();
        DateComboBox = new javax.swing.JComboBox();
        ValueComboBox = new javax.swing.JComboBox();
        ButtonConfirm = new javax.swing.JButton();
        LabelStartingTime = new javax.swing.JLabel();
        SpinnerTime = new javax.swing.JSpinner(new SpinnerDateModel());

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jDialog1Layout.setVerticalGroup(
            jDialog1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        LabelDate.setText("Select a date:");

        LabelValue.setText("Select a value: ");

        DateComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DateComboBoxActionPerformed(evt);
            }
        });

        ValueComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ValueComboBoxActionPerformed(evt);
            }
        });

        ButtonConfirm.setText("Confirm");
        ButtonConfirm.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ButtonConfirmActionPerformed(evt);
            }
        });

        LabelStartingTime.setText("Select starting time:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(ButtonConfirm))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(30, 30, 30)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(LabelStartingTime)
                                .addGap(146, 146, 146)
                                .addComponent(SpinnerTime))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(LabelDate)
                                    .addComponent(LabelValue))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 209, Short.MAX_VALUE)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(DateComboBox, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(ValueComboBox, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))))
                .addGap(59, 59, 59))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelDate)
                    .addComponent(DateComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(32, 32, 32)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelValue)
                    .addComponent(ValueComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(LabelStartingTime)
                    .addComponent(SpinnerTime, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(ButtonConfirm)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void ValueComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ValueComboBoxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_ValueComboBoxActionPerformed

    private void DateComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DateComboBoxActionPerformed

        ValueComboBox.removeAllItems();
        // Get selected date
        selectedDate = DateComboBox.getSelectedItem().toString() + folderEnding;
        initialiseValueComboBox(selectedDate);
    }//GEN-LAST:event_DateComboBoxActionPerformed

    private void ButtonConfirmActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ButtonConfirmActionPerformed
        if (DateComboBox.getSelectedItem() != null && ValueComboBox.getSelectedItem() != null && SpinnerTime.getValue() != null) {

            selectedDate = DateComboBox.getSelectedItem().toString() + folderEnding;
            selectedValue = ValueComboBox.getSelectedItem().toString() + fileExtension;
            selectedTime = new SimpleDateFormat("hh:mm:ss:SSS").format(SpinnerTime.getValue());

            // Notify Main controller
            Main.setSelectedFile(getSelectedFile());
            Main.setSelectedStartTime(getSelectedStartTime());
            // Send info to file reader
            dispose();
        }
    }//GEN-LAST:event_ButtonConfirmActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton ButtonConfirm;
    private javax.swing.JComboBox DateComboBox;
    private javax.swing.JLabel LabelDate;
    private javax.swing.JLabel LabelStartingTime;
    private javax.swing.JLabel LabelValue;
    private javax.swing.JSpinner SpinnerTime;
    private javax.swing.JComboBox ValueComboBox;
    private javax.swing.JDialog jDialog1;
    // End of variables declaration//GEN-END:variables

    private void initialiseDateComboBox(FileRead fileReader) {
        // create a combo box with items
        for (File dateFolder : fileReader.getDateFolders()) {
            String date = dateFolder.getName().replace(folderEnding, "");
            DateComboBox.addItem(date);
        }
    }

    private void initialiseValueComboBox(String selectedDate) {
        // create a combo box with items
        for (File valueFile : fileReader.getFilesFromFolder(selectedDate)) {
            String value = valueFile.getName().replace(fileExtension, "");
            ValueComboBox.addItem(value);
        }
    }

    public File getSelectedFile() {
        File fileSelected = null;
        if (selectedDate != null && selectedValue != null) {
            fileSelected = new File(ROOT_FOLDER_NAME + "\\" + selectedDate + "\\" + selectedValue);
        }
        return fileSelected;
    }

    public String getSelectedStartTime() {
        return this.selectedTime;
    }
}